package com.xingkeduo.business.operation.controller.cms;

import com.xingkeduo.business.operation.controller.InitController;
import com.xingkeduo.business.operation.domain.PermsDomain;
import com.xingkeduo.pojo.json.PermsJson;
import com.xingkeduo.pojo.query.PermsQuery;
import com.xingkeduo.pojo.table.PermsTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 菜单列表
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@RestController
@RequestMapping(CMS_URL_PREFIX +"/perms")
public class CPermsController extends InitController {

    @Autowired
    private PermsDomain permsDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            PermsQuery permsQuery = PermsJson.createQueryForPerms(urlParam);
            long id = permsQuery.getId();
            PermsTable permsTable = permsDomain.getPermsById(id);
            return returnUtil.getSuccess(PermsJson.toJsonOne(permsTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
    

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid PermsTable permsVo,ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            permsVo.setCreatedBy(this.getOperator(request));
            permsDomain.createPerms(permsVo);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid PermsTable permsVo  , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            PermsTable permsTable = PermsJson.createTableForModifyPerms(urlParam,permsVo);
            permsVo.setUpdatedBy(this.getOperator(request));
            permsDomain.modifyPerms(permsTable);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            PermsQuery permsQuery = PermsJson.createQueryForPerms(urlParam);
            long id = permsQuery.getId();
            permsDomain.delPerms(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public String doGetLists(PermsQuery permsQuery) {
        returnUtil = new ReturnUtil();
        try {
//            List<PermsTable> permsList= permsDomain.getMenuByPid(0L);
            Page<PermsTable> postsTablePage = permsDomain.getPostsPageList(permsQuery);
            return returnUtil.getSuccess(PermsJson.toJsonPage(postsTablePage));
            
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/getmenulists", method = RequestMethod.GET)
    public String doGetMenuLists() {
        returnUtil = new ReturnUtil();
        try {
            List<PermsTable> permsList= permsDomain.getMenuByPid(0L);
            return returnUtil.getSuccess(PermsJson.toJson(permsList));

        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

}
